package shop.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import shop.dao.CartDao;
import shop.dao.UserDao;
import shop.dao.impl.CartDaoImpl;
import shop.dao.impl.UserDaoImpl;
import shop.entity.User;
import shop.entity.dto.CartDTO;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private UserDao dao = new UserDaoImpl();
	private CartDao cartDao = new CartDaoImpl();
	
	//注册用户
	public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		//取参
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		User user = new User();
		user.setAccount(account);
		user.setEmail(email);
		user.setPassword(password);
		user.setPhone(phone);		
		user.setNickName("小白兔"+account);
		
		//调用dao方法
		dao.add(user);
		//跳转页面
		response.sendRedirect("login.jsp");
	}
	
	//用户登录
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		//取参
		String account = request.getParameter("account");//账号
		String password = request.getParameter("password");//密码
		String auto = request.getParameter("auto");//是否勾选自动登录
		if(auto!=null) {//勾选自动登录
			//将用户的账号和密码写入Cookie
			Cookie cookie1 = new Cookie("account",account);
			Cookie cookie2 = new Cookie("password",password);
			cookie1.setMaxAge(60*60*24*7);
			cookie2.setMaxAge(60*60*24*7);
			response.addCookie(cookie1);
			response.addCookie(cookie2);
		}
		
		User user = new User();
		user.setAccount(account);
		user.setPassword(password);
		
		//调用dao方法
		User user1 = dao.login(account,password);
		System.out.println(user1);
		//user1为空，表示数据库没有这个账号密码
		if(user1==null) {
			//登录失败存储错误信息并跳转到登陆页面
			session.setAttribute("msg", "用户名或密码错误！");
			response.sendRedirect("login.jsp");
		}else {
			//登录成功存储用户信息
			session.setAttribute("user", user1);
			//初始化购物车信息存储到session
			List<CartDTO> cartList = cartDao.findAll(user1.getId());
			session.setAttribute("cartList", cartList);
			//计算购物车商品总价存储到session
			double totalPrice = 0;
			for(CartDTO dto:cartList) {
				//累加计算购物车中商品的总价
				totalPrice += dto.getNum()*dto.getPrice();
			}
			session.setAttribute("totalPrice", totalPrice);
			
			String url = request.getParameter("url");
			if(url==null) {
				response.sendRedirect("index.jsp");
			}else {
				response.sendRedirect(url+".jsp");
			}
		}
			
			
	}
	
	//注销
	public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Cookie[] cookies = request.getCookies();
		//清除Cookie存储的账号密码
		for(Cookie cookie:cookies) {
			if("account".equals(cookie.getName())) {
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}
			if("password".equals(cookie.getName())) {
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}
			
		}
		//清除session存储的用户信息
		session.removeAttribute("user");
		response.sendRedirect("login.jsp");
	}
	
	//修改昵称
	public void changeNickName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String nickName = request.getParameter("nickName");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		dao.editNickName(nickName,user.getId());
		user.setNickName(nickName);
		session.setAttribute("user", user);
		response.sendRedirect("member.jsp");
	}
	
	//修改手机号
	public void editPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String newPhone = request.getParameter("newPhone");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		dao.editPhone(newPhone,user.getId());
		user.setPhone(newPhone);
		session.setAttribute("user", user);
		response.sendRedirect("memberSafe.jsp");
	}
	
	//修改邮箱
	public void editEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String newEmail = request.getParameter("newEmail");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		dao.editEmail(newEmail,user.getId());
		user.setEmail(newEmail);
		session.setAttribute("user", user);
		response.sendRedirect("memberSafe.jsp");
	}
	
	//修改密码
	public void editPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String newPassword = request.getParameter("newPassword");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		dao.editPassword(newPassword,user.getId());
		//修改密码后注销
		logout(request, response);
	}
	

}
